//
//  KLPushViewModel.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/20.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "KLPushViewModel.h"
#import "KLSystemInfo.h"
#import "KLMacro.h"
#import "KLIPush.h"
#import "KLModel.h"

@implementation KLPushViewModel
- (RACCommand*)turnOffPush{
    return KLRACCmd(^RACSignal *(id input) {
       return KLRACSignal(^RACDisposable *(id<RACSubscriber> subscriber) {
           [[UIApplication sharedApplication] unregisterForRemoteNotifications];
           return nil;
       });
    });
}
- (RACCommand*)registerPush{
    return KLRACCmd(^RACSignal *(id input) {
        return KLRACSignal(^RACDisposable *(id<RACSubscriber> subscriber) {
            if (IOS8_OR_LATER){
                [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings
                                                                                     settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge)
                                                                                     categories:nil]];
                [[UIApplication sharedApplication] registerForRemoteNotifications];
            }
            else{
                [[UIApplication sharedApplication] registerForRemoteNotificationTypes:
                 (UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)];
            }
            
            return nil;
        });
    });
}

- (RACCommand*)uploadDeviceToken:(NSData*)token{
    @weakify(self);
    return [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);;
        return [self.modelService uploadDeviceToken:token];
    }];
}
- (RACCommand*)bindUser:(id)userInfo{
    @weakify(self);
    return [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
       return [self.modelService bindUser:userInfo];
    }];
}
- (RACCommand*)unbindUser{
    @weakify(self);
    return [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self);
        return [self.modelService unbindUser];
    }];
}

//map userInfo to model cls and send to didReceiveRemoteNotification
- (RACCommand*)recvedRemoteNotificaton:(NSDictionary*)userInfo{
    @weakify(self);
    return [[RACCommand alloc] initWithSignalBlock:^RACSignal *(NSDictionary *userInfo) {
        @strongify(self);
        return [self recvedRemoteNotificatonSignal:userInfo];
    }];
}

//after cmd will call it
- (RACSignal*)recvedRemoteNotificatonSignal:(NSDictionary*)userInfo{
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id <RACSubscriber> subscriber) {
        @strongify(self);
        NSError *error = [self.model copyFromDictionary:userInfo];
        if(error){
            [subscriber sendError:error];
        }else{
            [subscriber sendNext:self.model];
        }

        return nil;
    }];
}


@end
